<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

include "header.php";
$page = "transactions";

//check user login
if (!$user->user_exists) {
    header('Location: ./login.php');
    exit();
}
//if($user->user_info['type_id'] != 1){
//    header('Location: ./locked.php');
//    exit();
//}

if (isset($_POST['page_number'])) {
    $page_number = $_POST['page_number'];
} elseif (isset($_GET['page_number'])) {
    $page_number = $_GET['page_number'];
} else {
    $page_number = 1;
}
if (isset($_POST['contract_code'])) {
    $contract_code = $_POST['contract_code'];
} elseif (isset($_GET['contract_code'])) {
    $contract_code = $_GET['contract_code'];
} else {
    $contract_code = "";
}

$params = array(
    'contract_code' => '',
    'house_name' => '',
    'room_number' => '',
    'client_name' => '',
    'client_phone' => '',
    'guarantee_company_name' => '',
    'content_id' => '',
    'reception_date' => '',
    'contract_cancel_date' => '',
    'stop_renting_date' => '',
    'stop_renting_time' => '',
    'way_to_see_room' => '',
    'report_fee_text' => '',
    'accept_report_fee' => '',
    'funiture_repair' => '',
    'recovery_key_date' => '',
    'new_registry_date' => '',
    'new_registry_content' => '',
    'new_registry_comment' => '',
    'payment_request_date' => '',
    'payment' => '',
    'status' => '',
    'appointment_change_key_date' => '',
    'assign_key_date' => '',
    'new_money_date' => '',
);
$params = array_merge($params,$_GET);

if (isset($_POST['status'])) {
    $status = $_POST['status'];
} elseif (isset($_GET['status'])) {
    $status = $_GET['status'];
} else {
    $status = "";
}

$class = new Transaction();

//calculator paging
$max = 25;
$totalItem = $class->getListItemsCount($params);

$totalPage = floor($totalItem / $max);

if ($totalItem % $max > 0){
    $totalPage = $totalPage + 1;
}
if ($page_number > $totalPage){
    $page_number = 1;
}

$offset = $page_number * $max - $max;
$length = $max;

$can_delete = false;
if($user->user_info['type_id'] == 1 && ($user->user_info['user_email'] == 'pm-info@am-bition.jp' || $user->user_info['user_email'] == 'bm-info@am-bition.jp')){
    $can_delete = true;
}

$smarty->assign('params', $params);
$smarty->assign("contents", $class->getContents());
$smarty->assign("repair_users", $class->getRepairUsers());
$smarty->assign("key_users", $class->getKeyUsers());

$smarty->assign('page_number', $page_number);
$smarty->assign('totalPage', $totalPage);
$smarty->assign('transactions', $class->getList(array_merge($params,array('offset' => $offset, 'length' => $length))));
$smarty->assign('viewer_type', $user->user_info['type_id']);
$smarty->assign('can_delete', $can_delete);

include "footer.php";
